package com.jieshuibao.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jieshuibao.common.bokecc.bean.LiveRoomStatus;
import com.jieshuibao.common.config.Global;
import com.jieshuibao.common.po.BaseRes;
import com.jieshuibao.model.db.Liveroom;
import com.jieshuibao.model.db.Tvbox;
import com.jieshuibao.service.BokeccService;
import com.jieshuibao.service.LiveroomService;
import com.jieshuibao.service.TvboxService;
import com.jieshuibao.util.StringUtils;

@Controller
@RequestMapping("liveroom")
public class LiveroomController {
	
	private static Logger log = Logger.getLogger(LiveroomController.class);

	@Resource
	private LiveroomService liveroomService;
	@Resource
	private BokeccService bokeccService;
	@Resource
	private TvboxService tvboxService;

	@RequestMapping("view")
	@ResponseBody
	public Object view(HttpServletRequest request) {
		BaseRes<Object> res = new BaseRes<Object>();
	/*	String userid = request.getHeader("cookieId");
		if(StringUtils.isEmpty(userid)){
			res.setCode("0");
			res.setDes("系统繁忙!");
			return res;
		}
		Tvbox tvboxByDeviceNo = tvboxService.getTvboxByDeviceNo(userid);
		if(null == tvboxByDeviceNo){
			res.setCode("0");
			res.setDes("没有权限观看!");
			return res;
		}*/
		List<Liveroom> rooms = liveroomService.selectByExample(null);
		if (null != rooms && rooms.size() > 0) {
			Liveroom liveroom = rooms.get(0);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("roomid", liveroom.getRoomId());
			map.put("name", liveroom.getName());
			map.put("cover", liveroom.getCover());
			LiveRoomStatus liveRoomStatus = bokeccService.liveRoomStatus(new String[] { liveroom.getRoomId() });
			if (null == liveRoomStatus) {
				res.setCode("0");
				res.setDes("获取直播间状态失败");
				return res;
			}
			int status = liveRoomStatus.getLiveStatus();
			String liveId = liveRoomStatus.getLiveId();
			map.put("status", status);
			map.put("liveId", liveId);
			res.setCode("1");
			res.setData(map);
			return res;
		} else {
			res.setCode("0");
			res.setDes("没有可用直播间");
			return res;
		}
	}

	@RequestMapping("verify")
	@ResponseBody
	public Object verify(HttpServletRequest request) {
		Map<String,String> param = new HashMap<String, String>();
		Enumeration enu=request.getParameterNames();  
		while(enu.hasMoreElements()){  
			String paraName=(String)enu.nextElement();  
			log.error("电视盒子端cc直播间校验:"+paraName+": "+request.getParameter(paraName));
			param.put(paraName, request.getParameter(paraName));
		}
		String userid = param.get("viewername");
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isEmpty(userid)){
			map.put("result", "no");
			map.put("message", "网络繁忙");
			return map;
		}
		Tvbox tvboxByDeviceNo = tvboxService.getTvboxByDeviceNo(userid);
		if(null == tvboxByDeviceNo){
			map.put("result", "no");
			map.put("message", "没有权限观看!");
			return map;
		}
		List<Liveroom> rooms = liveroomService.selectByExample(null);
		if(null != rooms && rooms.size() > 0){
		}else{
			map.put("result", "no");
			map.put("message", "没有可用直播间!");
			return map;
		}
		map.put("result", "ok");
		map.put("message", "请求成功");
		return map;
	}
}
